package com.example.a2205douyin.adapter;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.a2205douyin.R;
import com.example.a2205douyin.entity.MessageEntity;

import java.util.List;

public class MessageAdapter extends BaseQuickAdapter<MessageEntity, BaseViewHolder> {
    public MessageAdapter(int layoutResId, @Nullable List<MessageEntity> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, MessageEntity entity) {
        String str = entity.getUsername()+":"+entity.getMsg();
        //富文本
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        builder.setSpan(new ForegroundColorSpan(getContext().getColor(R.color.red)),0,entity.getUsername().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(getContext().getColor(R.color.teal_200)),entity.getUsername().length()+1,str.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        holder.setText(R.id.tv,builder);
    }
}
